<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      #some-element-you-want-to-animate {
        width: 100px;
        height: 100px;
        background-color: blueviolet;
        transition: all 3s linear 0.2s;
      }
    </style>
  </head>
  <body>
    <div id="some-element-you-want-to-animate"></div>
  </body>
  <script>
    const element = document.getElementById('some-element-you-want-to-animate');
    let start, previousTimeStamp;
    let done = false;

    function step(timestamp) {
        console.info('step--',timestamp)
      if (start === undefined) {
        start = timestamp;
      }
      const elapsed = timestamp - start;
      if (previousTimeStamp !== timestamp) {
        // 这里使用 Math.min() 确保元素在恰好位于 200px 时停止运动
        const count = Math.min(0.1 * elapsed, 200);
        element.style.transform = `translateX(${count}px)`;
        if (count === 200) done = true;
      }

      if (elapsed < 2000) {
        // 2 秒之后停止动画
        previousTimeStamp = timestamp;
        if (!done) {
          window.requestAnimationFrame(step);
        }
      }
    }
    window.requestAnimationFrame(step);
  </script>
</html>
